package com.marvels.financial.loan.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.DateUtils;
import com.marvels.common.utils.ServerProperties;
import com.marvels.financial.loan.dao.McfeBusinessOverviewDao;
import com.marvels.financial.loan.dto.AnalysisStatisticalConditionDTO;
import com.marvels.financial.loan.dto.BusinessAnalysisTable1DTO;
import com.marvels.financial.loan.dto.BusinessAnalysisTable2DTO;
import com.marvels.financial.loan.dto.BusinessOverviewDataDTO;
import com.marvels.financial.loan.dto.PersonalPerformanceParamDTO;
import com.marvels.financial.loan.dto.PersonalPerformanceResultDTO;
import com.marvels.financial.loan.service.AnalysisStatisticalApi;

import cn.hutool.core.util.StrUtil;

@Service
public class AnalysisStatisticalImpl implements AnalysisStatisticalApi{

	@Autowired
	private McfeBusinessOverviewDao mcfeBusinessOverviewDao;
	
	@Value("${config.properties.path}")
	private String configPropertiesPath;
	
	@Override
	public BusinessOverviewDataDTO businessOverview(AnalysisStatisticalConditionDTO analysisStatisticalConditionDTO) {
		return mcfeBusinessOverviewDao.businessOverview(analysisStatisticalConditionDTO);
	}

	@Override
	public Map<String, Object>  businessAnalysisTable1(AnalysisStatisticalConditionDTO conditionDTO)
			throws MarvelsBusinessException {
		if(conditionDTO.getBegindDate()==null){//开始时间为空
			throw new MarvelsBusinessException("请选择开始日期");
		}
		if(conditionDTO.getEndDate()==null){
			throw new MarvelsBusinessException("请选择结束日期");
		}
		Date beginDate=DateUtils.getDateYYYY_MM_DD(conditionDTO.getBegindDate());
		Date enDate=DateUtils.getDateYYYY_MM_DD(conditionDTO.getEndDate());
		conditionDTO.setEndDate(DateUtils.getDateYYYY_MM_DD(DateUtils.add(enDate, Calendar.DAY_OF_YEAR, 1) ));
		BusinessAnalysisTable1DTO staticDTO=mcfeBusinessOverviewDao.businessAnalysisTable1(conditionDTO);
		staticDTO.setNetIncome((staticDTO.getCostRecovery().subtract(staticDTO.getRepaymentPrincipal()).add(staticDTO.getRepaymentedPrincipal())).setScale(2, BigDecimal.ROUND_HALF_UP));
		staticDTO.setSummary((staticDTO.getUnexpired().add(staticDTO.getDisRepaymentM1()).add(staticDTO.getDisRepaymentM2())).setScale(2, BigDecimal.ROUND_HALF_UP));
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("static", staticDTO);
		List<BusinessAnalysisTable1DTO>   list=new ArrayList<BusinessAnalysisTable1DTO>();
		for(Date tmp=beginDate;enDate.compareTo(tmp)>=0;tmp=DateUtils.add(tmp, Calendar.DAY_OF_YEAR, 1)){
			conditionDTO.setBegindDate(DateUtils.getDateYYYY_MM_DD(tmp));
			conditionDTO.setEndDate(DateUtils.getDateYYYY_MM_DD(DateUtils.add(tmp, Calendar.DAY_OF_YEAR, 1) ));
			BusinessAnalysisTable1DTO  tmpDTO = mcfeBusinessOverviewDao.businessAnalysisTable1(conditionDTO);
			tmpDTO.setLoanDate(tmp);
			tmpDTO.setNetIncome((tmpDTO.getCostRecovery().subtract(tmpDTO.getRepaymentPrincipal())).setScale(2, BigDecimal.ROUND_HALF_UP));
			tmpDTO.setSummary((tmpDTO.getUnexpired().add(tmpDTO.getDisRepaymentM1()).add(tmpDTO.getDisRepaymentM2())).setScale(2,BigDecimal.ROUND_HALF_UP));
			list.add(tmpDTO);
		}
		map.put("list", list);
		return map;
	}

	@Override
	public Map<String, Object>  businessAnalysisTable2(AnalysisStatisticalConditionDTO conditionDTO)
			throws MarvelsBusinessException {
		if(conditionDTO.getBegindDate()==null){//开始时间为空
			throw new MarvelsBusinessException("请选择开始日期");
		}
		if(conditionDTO.getEndDate()==null){
			throw new MarvelsBusinessException("请选择结束日期");
		}
		Date beginDate=DateUtils.getDateYYYY_MM_DD(conditionDTO.getBegindDate());
		Date enDate=DateUtils.getDateYYYY_MM_DD(conditionDTO.getEndDate());
		List<BusinessAnalysisTable2DTO>   list=new ArrayList<BusinessAnalysisTable2DTO>();
		for(Date tmp=beginDate;enDate.compareTo(tmp)>=0;tmp=DateUtils.add(tmp, Calendar.DAY_OF_YEAR, 1)){
			conditionDTO.setBegindDate(DateUtils.getDateYYYY_MM_DD(tmp));
			conditionDTO.setEndDate(DateUtils.getDateYYYY_MM_DD(DateUtils.add(tmp, Calendar.DAY_OF_YEAR, 1)));
			BusinessAnalysisTable2DTO  tmpDTO = mcfeBusinessOverviewDao.businessAnalysisTable2(conditionDTO);
			tmpDTO.setLoanDate(tmp);
			list.add(tmpDTO);
		}
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("list", list);
		return map;
	}

	@Override
	public List<PersonalPerformanceResultDTO> queryPersonalPerformanceList(PersonalPerformanceParamDTO personalPerformanceParamDTO) throws MarvelsBusinessException{
		try {
			PageHelper.startPage(personalPerformanceParamDTO.getPageNo(), personalPerformanceParamDTO.getPageSize());
			String ignoreUserCodes= ServerProperties.getValue(configPropertiesPath, "common.properties", "ignore_user_code");
			if(StrUtil.isNotEmpty(ignoreUserCodes)){
				personalPerformanceParamDTO.setIgnoreUserCodeList(Arrays.asList(ignoreUserCodes.split(",")));
			}
			return mcfeBusinessOverviewDao.queryPersonalPerformanceList(personalPerformanceParamDTO);
		} catch (Exception e) {
			throw new MarvelsBusinessException(e);
		}
	}

	
}
